package com.hundsun.cprs.yyt.web.action.system;

import com.hundsun.cprs.yyt.service.common.dto.AreaInfo;
import com.hundsun.cprs.yyt.service.common.service.RemoteCommonService;
import com.hundsun.cprs.yyt.service.dilivery.domain.DiliveryAddress;
import com.hundsun.cprs.yyt.service.dilivery.domain.query.AddressQuery;
import com.hundsun.cprs.yyt.service.dilivery.service.DiliveryAddressService;
import com.hundsun.cprs.yyt.web.action.BaseAction;
import com.hundsun.cprs.yyt.web.vo.JsonModel;
import org.apache.commons.lang.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("selfGet/address/")
public class AddressAction extends BaseAction {

    @Autowired
    private DiliveryAddressService diliveryAddressService;
    @Autowired
    private RemoteCommonService remoteCommonService;

    @RequestMapping("list.htm")
    public String addressList(@ModelAttribute("query")AddressQuery query){
        query.setUserId(0L);//自提地址userId为0
        diliveryAddressService.queryAddressByPage(query);
        return "system/address/list";
    }

    @RequestMapping(value = "add.htm",method = RequestMethod.GET)
    public String addInit(ModelMap model){
        model.addAttribute("provinceList",remoteCommonService.getProvinces());
        return "system/address/add";
    }

    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ResponseBody
    public JsonModel add(DiliveryAddress diliveryAddress){
        try{
            Validate.notEmpty(diliveryAddress.getProvinceAddress(),"省级地址必填");
            Validate.notEmpty(diliveryAddress.getProvinceCode(),"省级地址必填");
            Validate.notEmpty(diliveryAddress.getMunicipalAddress(),"市级地址必填");
            Validate.notEmpty(diliveryAddress.getMunicipalCode(),"市级地址必填");
            Validate.notEmpty(diliveryAddress.getCountyAddress(),"区级地址必填");
            Validate.notEmpty(diliveryAddress.getCountyCode(),"区级地址必填");
            Validate.notEmpty(diliveryAddress.getDiliveryAddress(),"详细地址必填");
            diliveryAddress.setUserId(0L);//自提地址用户id为0
            diliveryAddressService.createDiliveryAddress(diliveryAddress);
            return new JsonModel(JsonModel.RESULD_CODE_SUCCESS,"自提地址新增成功");
        }catch (IllegalArgumentException e){
            log.error(e);
            return new JsonModel(JsonModel.RESULD_CODE_FAIL,e.getMessage());
        }catch (Exception e){
            log.error(e);
            return new JsonModel(JsonModel.RESULD_CODE_FAIL,"自提地址新增失败");
        }
    }

    @RequestMapping("city.json")
    @ResponseBody
    public JsonModel getCitys(Integer provinceId){
        List<AreaInfo> areaInfos = new ArrayList<>();
        try{
            areaInfos = remoteCommonService.getCitysByProvince(provinceId);
            return new JsonModel(JsonModel.RESULD_CODE_SUCCESS,"success",areaInfos);
        }catch (Exception e){
            log.error(e);
            return new JsonModel(JsonModel.RESULD_CODE_FAIL,"fail",areaInfos);
        }
    }

    @RequestMapping("county.json")
    @ResponseBody
    public JsonModel getCountys(Integer cityId){
        List<AreaInfo> areaInfos = new ArrayList<>();
        try{
            areaInfos = remoteCommonService.getAreasByCity(cityId);
            return new JsonModel(JsonModel.RESULD_CODE_SUCCESS,"success",areaInfos);
        }catch (Exception e){
            log.error(e);
            return new JsonModel(JsonModel.RESULD_CODE_FAIL,"fail",areaInfos);
        }
    }

    @RequestMapping("del.json")
    @ResponseBody
    public JsonModel del(@RequestParam("addressId")Long addressId){
        try{
            diliveryAddressService.deleteDiliveryAddress(0L,addressId);
            return new JsonModel(JsonModel.RESULD_CODE_SUCCESS,"删除成功");
        }catch (Exception e){
            log.error(e);
            return new JsonModel(JsonModel.RESULD_CODE_FAIL,"删除失败");
        }
    }

    @RequestMapping(value = "edit",method = RequestMethod.GET)
    public String editInit(@RequestParam("addressId") Long addressId,ModelMap model){
        model.addAttribute("provinceList",remoteCommonService.getProvinces());
        model.addAttribute("diliveryAddress",diliveryAddressService.selectAddressDetailByAddressId(addressId));
        return "system/address/edit";
    }

    @RequestMapping(value = "edit",method = RequestMethod.POST)
    @ResponseBody
    public JsonModel edit(@ModelAttribute("diliveryAddress")DiliveryAddress diliveryAddress){
        try{
            Validate.notEmpty(diliveryAddress.getProvinceAddress(),"省级地址必填");
            Validate.notEmpty(diliveryAddress.getProvinceCode(),"省级地址必填");
            Validate.notEmpty(diliveryAddress.getMunicipalAddress(),"市级地址必填");
            Validate.notEmpty(diliveryAddress.getMunicipalCode(),"市级地址必填");
            Validate.notEmpty(diliveryAddress.getCountyAddress(),"区级地址必填");
            Validate.notEmpty(diliveryAddress.getCountyCode(),"区级地址必填");
            Validate.notEmpty(diliveryAddress.getDiliveryAddress(),"详细地址必填");
            diliveryAddress.setUserId(0L);//自提地址用户id为0
            diliveryAddressService.modifiyDiliveryAddress(diliveryAddress);
            return new JsonModel(JsonModel.RESULD_CODE_SUCCESS,"修改成功");
        }catch (Exception e){
            log.error(e);
            return new JsonModel(JsonModel.RESULD_CODE_FAIL,"修改失败");
        }
    }
}
